#COMMON

name := common
Program := commontest
Include := -I. -I./common -I./gtest -I./common_test

LIBS :=  -lpthread -lrt
CPP := g++
CC  := g++
MACRO_DEFINE := -D__linux__ -DirixMipsPro -D__PTHREADS__ -D__PTHREADS_MUTEXES__ -DBIGENDIAN -DUSE_THREAD -DEXPORT -D_REENTRANT
CPPFLAGS  := -Wall -Werror $(Include) -w $(INCLUDE_FLAG) $(MACRO_DEFINE) -static
CFLAGS := $(CPPFLAGS) 
RM := rm -f

SRCDIRS := .
SRCEXTS := .cpp
SOURCES := $(foreach d,$(SRCDIRS),$(wildcard $(addprefix $(d)/*,$(SRCEXTS))))
OBJS    := $(foreach x,$(SRCEXTS),$(patsubst %$(x),%.o,$(filter %$(x),$(SOURCES))))
DEPS    := $(patsubst %.o,%.d,$(OBJS))

COMMONDIRS := ./common
COMMONEXTS := .cpp
COMMONSOURCES := $(foreach d,$(COMMONDIRS),$(wildcard $(addprefix $(d)/*,$(COMMONEXTS))))
COMMONOBJS    := $(foreach x,$(COMMONEXTS),$(patsubst %$(x),%.o,$(filter %$(x),$(COMMONSOURCES))))
COMMONDEPS    := $(patsubst %.o,%.d,$(COMMONOBJS))
COMMONOBJSLINK := $(patsubst %.o,$(COMMONDIRS)/%.o,$(COMMONOBJS))
COMMONLIB := ./lib/common.a

GTESTDIRS := ./gtest
GTESTEXTS := .cc
GTESTSOURCES := $(foreach d,$(GTESTDIRS),$(wildcard $(addprefix $(d)/*,$(GTESTEXTS))))
GTESTOBJS    := $(foreach x,$(GTESTEXTS),$(patsubst %$(x),%.o,$(filter %$(x),$(GTESTSOURCES))))
GTESTDEPS    := $(patsubst %.o,%.d,$(GTESTOBJS))
GTESTOBJSLINK := $(patsubst %.o,$(GTESTDIRS)/%.o,$(GTESTOBJS))
GTESTLIB := ./lib/gtest.a


COMMON_TEST_DIRS := ./common_test
COMMON_TEST_EXTS := .cpp
COMMON_TEST_SOURCES := $(foreach d,$(COMMON_TEST_DIRS),$(wildcard $(addprefix $(d)/*,$(COMMON_TEST_EXTS))))
COMMON_TEST_OBJS    := $(foreach x,$(COMMON_TEST_EXTS),$(patsubst %$(x),%.o,$(filter %$(x),$(COMMON_TEST_SOURCES))))
COMMON_TEST_DEPS    := $(patsubst %.o,%.d,$(COMMON_TEST_OBJS))
COMMON_TEST_OBJSLINK := $(patsubst %.o,$(COMMON_TEST_DIRS)/%.o,$(COMMON_TEST_OBJS))
COMMON_TEST_LIB := ./lib/common_test.a


.PHONY : all objs commonlib gtestlib clean cleangtest cleancommon rebuild cleanall
all:$(Program)
%.d : %.c
	@echo D... $<
	@$(CC) -MM -MD $(CFLAGS) $<
%.d : %.C
	@echo D... $<
	@$(CC) -MM -MD $(CFLAGS) $<
%.d : %.cpp
	@echo D... $<
	@$(CC) -MM -MD $(CPPFLAGS) $<
%.d : %.CPP
	@echo D... $<
	@$(CC) -MM -MD $(CPPFLAGS) $<
%.d : %.cc
	@echo D... $<
	@$(CC) -MM -MD $(CPPFLAGS) $<
%.d : %.CC
	@echo D... $<
	@$(CC) -MM -MD $(CPPFLAGS) $<

objs:$(OBJS)
%.o : %.c
	@echo Compiling... $<
	@$(CC) $(CFLAGS) -c $< -o $(patsubst %.c,%.o,$<)
%.o : %.C
	@echo Compiling... $<
	@$(CC) $(CFLAGS) -c $< -o $(patsubst %.C,%.o,$<)
%.o : %.cpp
	@echo Compiling... $<
	@$(CPP) $(CPPFLAGS) -c $< -o $(patsubst %.cpp,%.o,$<)
%.o : %.CPP
	@echo Compiling... $<
	@$(CPP)  $(CPPFLAGS) -c $< -o $(patsubst %.CPP,%.o,$<)
%.o : %.cc
	@echo Compiling... $<
	@$(CPP) $(CPPFLAGS) -c $< -o $(patsubst %.cc,%.o,$<)
%.o : %.CC
	@echo Compiling... $<
	@$(CPP)  $(CPPFLAGS) -c $< -o $(patsubst %.CC,%.o,$<)

#commonlib:$(COMMONOBJS)

#gtestlib:$(GTESTOBJS)

$(Program) : $(COMMONOBJS) $(GTESTOBJS) $(OBJS) $(COMMON_TEST_OBJS)
	$(CC) -o $(Program) $(OBJS) $(COMMONOBJS) $(COMMON_TEST_OBJS)  $(GTESTOBJS) $(LINKOPTS) $(LIBS)

-include $(DEPS)

clean:
	@$(RM) *.o *.log
cleangtest:
	@$(RM) $(GTESTDIRS)/*.o
cleancommon:
	@$(RM) $(COMMONDIRS)/*.o 
cleancommontest:
	@$(RM) $(COMMON_TEST_DIRS)/*.o 
cleanall: clean cleangtest cleancommon cleancommontest

rebuild:cleanall all

run:
	@./$(Program)

show:
	@echo $(SOURCES)
	@echo $(OBJS)
	@echo $(COMMONSOURCES)
	@echo $(COMMONOBJS)
	@echo $(GTESTSOURCES)
	@echo $(GTESTOBJS)
	@echo $(DEPS)
	@echo $(LIB)
	@echo $(Include)
	@echo $(CPPFLAGS)
